from flask import Blueprint, request
from service import bookService
from service.bookService import *
from flask_cors import cross_origin

book_bp = Blueprint('book_bp', __name__)


@book_bp.route('/', methods=['GET'])
def getAllBook():
    return getAllBookList()


@book_bp.route('/<id>', methods=['GET'])
def getOneBook(id):
    print(id)
    return bookService.getOneBook(id)


@book_bp.route('/find/<name>', methods=['GET'])
def getBookByName(name):
    print(name)
    return bookService.getBookByNmae(name)


@book_bp.route('/', methods=['POST'])
def createBook():
    data = request.get_json()
    print(data['num'])
    book = Book(data['name'], data['author'], data['introduction'], data['picture'], data['num'],
                data['price'], data['score'], data['sort'], data['code'], data['location'])
    print(book.num)
    if 'id' in data:
        return updateBook(data['id'], book)
    return addBook(book)


@book_bp.route('/delete', methods=['POST'])
def deleteBook():
    data = request.get_json()
    id = data['id']
    print(id)
    return bookService.deleteBook(id)
